package com.hsmw.common.base;

import cn.hutool.core.convert.Converter;
import cn.hutool.core.convert.ConverterRegistry;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import static com.baomidou.mybatisplus.annotation.IdType.ASSIGN_ID;
import static com.baomidou.mybatisplus.annotation.IdType.INPUT;


/**
 * @author hongfei
 */
@Getter
@Setter
@Slf4j
public abstract class BaseModel implements Serializable {

	private static Map<String, DateTimeFormatter> formatterMap = new HashMap<>();

	static {
		formatterMap.put("yyyy-MM-dd'T'HH:mm:ss.SSS", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
		formatterMap.put("yyyy-MM-dd'T'HH:mm:ss", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
		formatterMap.put("yyyy-MM-dd'T'HH:mm", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"));
		formatterMap.put("yyyy-MM-dd HH:mm:ss.SSS", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
		formatterMap.put("yyyy-MM-dd HH:mm:ss", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		formatterMap.put("yyyy-MM-dd HH:mm", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
		try {
			ConverterRegistry.getInstance().putCustom(LocalDateTime.class, (Converter) (value, defaultValue) -> {
				String strValue = String.valueOf(value);
				if (strValue.contains("T")) {
					if (strValue.matches("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}$")) {
						return LocalDateTime.from(formatterMap.get("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(String.valueOf(value)));
					} else if (strValue.matches("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$")) {
						return LocalDateTime.from(formatterMap.get("yyyy-MM-dd'T'HH:mm:ss").parse(String.valueOf(value)));
					} else if (strValue.matches("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}$")) {
						return LocalDateTime.from(formatterMap.get("yyyy-MM-dd'T'HH:mm").parse(String.valueOf(value)));
					}
				} else {
					if (strValue.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}$")) {
						return LocalDateTime.from(formatterMap.get("yyyy-MM-dd HH:mm:ss.SSS").parse(String.valueOf(value)));
					} else if (strValue.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$")) {
						return LocalDateTime.from(formatterMap.get("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(value)));
					} else if (strValue.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}$")) {
						return LocalDateTime.from(formatterMap.get("yyyy-MM-dd HH:mm").parse(String.valueOf(value)));
					}
				}
				log.warn("[LocalDateTime Converter]invalid time format : " + strValue);
				return null;
			});
		} catch (Exception ignore) {
		}
	}

	private static final long serialVersionUID = 8149988578999467520L;

	@TableId(type = IdType.ASSIGN_ID)
	private String id;
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(fill = FieldFill.INSERT)
	private LocalDateTime createTime;
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@TableField(fill = FieldFill.INSERT_UPDATE)
	private LocalDateTime updateTime;
	private String updateUser;
	private String createUser;
}
